package com.qf2210.group.three.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.qf2210.group.three.pojo.Good;
import com.qf2210.group.three.server.mapper.GoodMapper;
import com.qf2210.group.three.server.service.GoodService;

import com.qf2210.group.three.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liqing
 * @description 针对表【tb_good】的数据库操作Service实现
 * @createDate 2022-12-01 21:36:14
 */
@Service
public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good>
        implements GoodService {

    @Override
    public Page<Good> findGoodsWithPage(int cId, int bId, Page<Good> page) {
        int counts = this.baseMapper.getCounts(cId, bId);//总记录数
        int allPages = counts % page.getPageSize() == 0 ? counts / page.getPageSize() : counts / page.getPageSize() + 1;
        int start = page.getPageNum() <= 1 ? 0 : (page.getPageNum() - 1) * page.getPageSize();
        int end = page.getPageSize();
        List<Good> goods = this.baseMapper.getGoods(cId, bId, start, end);
        Page<Good> goodPage = new Page<>();
        List<Good> prepage;
        List<Good> nextpage;

        if (page.getPageNum() <= 1) {

            prepage=null;
            nextpage = this.baseMapper.getGoods(cId, bId, (page.getPageNum() - 1 + 1) * page.getPageSize(), end);
            goodPage.setPrePage(null);
        } else if (page.getPageNum() >= allPages) {

            nextpage=null;
            prepage = this.baseMapper.getGoods(cId, bId, (page.getPageNum() - 1 - 1) * page.getPageSize(), end);
            goodPage.setNextPage(null);
        }else {
            nextpage = this.baseMapper.getGoods(cId, bId, (page.getPageNum() - 1 + 1) * page.getPageSize(), end);
            prepage = this.baseMapper.getGoods(cId, bId, (page.getPageNum() - 1 - 1) * page.getPageSize(), end);
        }
        goodPage.setPageNum(page.getPageNum());
        goodPage.setCount(counts);
        goodPage.setPrePage(new Page<Good>(page.getPageNum() - 1, allPages, counts, null, null, prepage, page.getPageSize()));
        goodPage.setNextPage(new Page<Good>(page.getPageNum() + 1, allPages, counts, null, null, nextpage, page.getPageSize()));
        goodPage.setList(goods);
        goodPage.setPageSize(page.getPageSize());
        goodPage.setAllPages(allPages);//总页数
        return goodPage;
    }


}




